package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.conn.JsonHotwordsAsyGet;

import java.util.List;

/**
 * Created by Administrator on 2015/8/27 0027.
 */
public abstract class SearchAdapter extends QSAdapter<JsonHotwordsAsyGet.Info.Word> {

    private LayoutInflater layoutInflater;

    public SearchAdapter(Context context, List<JsonHotwordsAsyGet.Info.Word> objects) {

        super(context, objects);

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final JsonHotwordsAsyGet.Info.Word word = getItem(position);

        TextView title;

        if(convertView == null){

            convertView = QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_search, null));

            convertView.setTag(title = (TextView) convertView.findViewById(R.id.item_search_title));

        }else{

            title = (TextView) convertView.getTag();

        }

        title.setText(word.title);

        title.setBackgroundResource(word.isSelect ? R.drawable.shape_radius_solid_blue_blue : R.drawable.shape_radius_solid_null_gray);

        title.setTextColor(Color.parseColor(word.isSelect ? "#ffffff" : "#666666"));

        title.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                for (int i = 0 ; i < getCount() ; i++){

                    getItem(i).isSelect = false;

                }

                word.isSelect = true;

                onItemClick(word);

                notifyDataSetChanged();

            }

        });

        return convertView;

    }

    protected abstract void onItemClick(JsonHotwordsAsyGet.Info.Word word);

}
